package kr.saeildamil.androidstudy_second.gesture;

import java.util.ArrayList;

import kr.saeildamil.androidstudy_second.R;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class GestureOverlay extends Activity {
	WebView mWeb;
	GestureLibrary mLIbrary;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch31_goverlay);
		
		mWeb = (WebView)findViewById(R.id.web);
		mWeb.setWebViewClient(new MyWebClient());
		WebSettings set = mWeb.getSettings();
		set.setJavaScriptEnabled(true);
		set.setBuiltInZoomControls(true);
		mWeb.loadUrl("http://www.google.com");
		
		mLIbrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
		if(!mLIbrary.load() == false){
			finish();
		}
		GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
		gestures.addOnGesturePerformedListener(mListener);
	}
	
	class MyWebClient extends WebViewClient	{
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			return super.shouldOverrideUrlLoading(view, url);
		}
	}
	
	OnGesturePerformedListener mListener = new OnGesturePerformedListener() {
		
		@Override
		public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
			ArrayList<Prediction> predictions = mLIbrary.recognize(gesture);
			
			if(predictions.size()!=0){
				Prediction prediction = predictions.get(0);
				String name = prediction.name;
				
				if(prediction.score>1.0){
					if(name.equalsIgnoreCase("kiyuk")){
						mWeb.goBack();
					}else if(name.equalsIgnoreCase("niun")){
						mWeb.goForward();
					}else if(name.equalsIgnoreCase("digut")){
						mWeb.loadUrl("http://www.naver.com");
					}else{
						mWeb.loadUrl("http://www.daum.net");
					}
		
				}
			}
			
		}
	};
}
